package com.globaldom.web.stats;

import java.util.Date;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpSession;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.globaldom.utils.PMF;
import com.globaldom.web.ResourceController;

//@Controller
//@RequestMapping(value = "/statsupdater")
public class StatsUpdateController {
	public StatsUpdateController() {
	}
	private static final Logger log = Logger.getLogger(ResourceController.class.getName());
	// initializing method
	@RequestMapping(method = RequestMethod.GET)
	public boolean updateAllStats(Model model, HttpSession session) {

		PersistenceManager pmf = PMF.get().getPersistenceManager();
		try {
			Query query = pmf.newQuery("DELETE FROM Gift where giftReceiver==null && createDate<=yesterday");
			query.declareParameters("date yesterday");
			Date d = new Date();
			long time = d.getTime();
			time -= (24 * 60 * 60 * 1000);
			query.execute(new Date(time));
		} catch (Exception ex) {
			log.severe("Unable to run the status updater log" + ex.getMessage());
			ex.printStackTrace();
		}

		return true;
	}
}
